<?php
class PostsController extends AppController {
	var $name = 'Posts';
	var $authAllow = array('index', 'view');
	
	function index(){
		$posts = $this->Post->findAll();
		$this->set(compact('posts'));
	}
	
	function view(){
		if(!($this->Post->id = func_get_arg(0)) || !$this->Post->exists()){
			$this->redirect(array('action' => 'index'));
		}
		$this->Post->recursive = 2;
		$this->set('post', $this->Post->read());
	}
	
	function respond(){
		if(!empty($this->data)){
			if($this->Post->Comment->save($this->data)){
				$this->Session->setFlash('Comment saved!');
				$this->redirect($this->referer());
			}
		}
	}
	
	function admin_index(){
		$posts = $this->Post->findAll();
		$this->set(compact('posts'));
		$this->pageTitle = 'Manage Posts';
	}
	
	function admin_add(){
		$tags = $this->Post->Tag->find('list');
		$this->set(compact('tags'));
		if(!empty($this->data)){
			if($this->Post->save($this->data)){
				$this->Session->setFlash('Successfully saved.');
				$this->redirect(array('action' => 'index'));
			}
		}
		$this->pageTitle = 'Add Post';
		$this->set('action', 'add');
		$this->render('admin_form');
	}
	
	function admin_edit(){
		if(func_num_args() != 1 || !is_numeric(func_get_arg(0)) || !($this->{$this->modelClass}->id = intval(func_get_arg(0))) || !$this->{$this->modelClass}->exists()){
			$this->Session->setFlash('No such post.');
			$this->redirect(array('action' => 'index'));
		}
		$tags = $this->Post->Tag->find('list');
		$this->set(compact('tags'));
		if(!empty($this->data)){
			if($this->Post->save($this->data)){
				$this->Session->setFlash('Successfully saved.');
				$this->redirect(array('action' => 'index'));
			}
		}
		else{
			$this->data = $this->{$this->modelClass}->read();
		}
		$this->pageTitle = 'Edit Post';
		$this->set('action', 'edit');
		$this->render('admin_form');
	}
}
?>